<article id="wikiArticle">
<div><font><font> <div class="blockIndicator deprecated deprecatedHeader">
<p><strong><span class="icon-only-inline" title="This is an obsolete API and is no longer guaranteed to work."><i class="icon-trash"> </i></span> 已废弃</strong><br/>该特性已经从 Web 标准中删除，虽然一些浏览器目前仍然支持它，但也许会在未来的某个时间停止支持，请尽量不要使用该特性。</p>
</div></font></font></div>
<p><strong><code>fontcolor()</code></strong><font><font>方法创建一个<a href="/zh-CN/docs/Web/HTML/Element/font" title="HTML Font 元素（&lt;font&gt;）定义了该内容的字体大小、顏色与表现。"><code>&lt;font&gt;</code></a>的HTML元素让字符串被显示成指定的字体颜色。</font></font></p>
<div class="note">
<p><strong><font><font>使用注意：</font></font></strong><font><font>&lt;font&gt;元素已经在在</font></font><a href="/en-US/docs/Web/Guide/HTML/HTML5"><font><font>HTML5中</font></font></a><font><font>被移除并且不应该在使用。</font><font>替代的是，web开发者应该使用</font></font><a href="/en-US/docs/Web/CSS"><font><font>CSS</font></font></a><font><font>属性。</font></font></p>
</div>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code><var>str</var>.fontcolor(<var>color</var>)</code></code></pre>
<h3 id="参数"><font><font>参数</font></font></h3>
<dl>
<dt><code>color</code></dt>
<dd><font><font>代表颜色的一个字符串，可以是三个一组的十六进制的RGB值，也可以是一个颜色名称的字符串字面量，</font><font>颜色名称的字符串字面量被列在了这里</font></font><a href="/en-US/docs/Web/CSS/color_value"><font><font>CSS颜色参考</font></font></a><font><font>。</font></font></dd>
</dl>
<h3 id="返回值"><font><font>返回值</font></font></h3>
<dl>
<dd><font><font>一个包含一个<a href="/zh-CN/docs/Web/HTML/Element/font" title="HTML Font 元素（&lt;font&gt;）定义了该内容的字体大小、顏色与表现。"><code>&lt;font&gt;</code></a> HTML元素的字符串。</font></font></dd>
</dl>
<h2 id="描述"><font><font>描述</font></font></h2>
<p><font><font>如果你表示的颜色为十六进制RGB三原色，则必须使用的格式</font></font><code>rrggbb</code><font><font>。</font><font>例如，对于橙红色的十六进制RGB值是红色=FA，绿色=80，和蓝=72，所以橙红色的RGB三原色</font></font><code>"FA8072"</code><font><font>。</font></font></p>
<h2 id="例子"><font><font>例子</font></font></h2>
<h3 id="使用_fontcolor()"><font><font>使用 </font></font><code>fontcolor()</code></h3>
<p><font><font>下面的示例使用</font></font><code>fontcolor()</code><font><font>方法来改变字符串的颜色，通过产生一个被HTML &lt;font&gt; 标签包裹的字符串</font></font>。</p>
<pre><code  class="language-javascript"><font><font>var worldString = "Hello, world"</font></font>
<font><font>
console.log(worldString.fontcolor('red') + ' is red in this line');
// &lt;font color="red"&gt;Hello, world &lt;/font&gt; is red in this line"</font></font>
<font><font>
console.log（worldString.fontcolor('FF00') + ' is red in hexadecimal'</font></font><font><font>
// &lt;font color="FF00"&gt;Hello，world &lt;/font&gt; is red in hexadecimal</font></font>
</code></pre>
<p><font><font>和<a href="/zh-CN/docs/Web/API/HTMLElement/style" title="HTMLElement.style 属性返回一个 CSSStyleDeclaration 对象，表示元素的 内联style 属性（attribute），但忽略任何样式表应用的属性。 通过 style 可以访问的 CSS 属性列表，可以查看 CSS Properties Reference。"><code>element.style</code></a>对象一起，你可以访问元素的</font></font><code>style</code><font><font>属性，并且更随意的去操纵它，例如：</font></font></p>
<pre><code  class="language-javascript"><font><font>document.getElementById('yourElemId').style.color ='red';
</font></font></code></pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col"><font><font>规范</font></font></th>
<th scope="col"><font><font>状态</font></font></th>
<th scope="col"><font><font>评论</font></font></th>
</tr>
<tr>
<td><font><font><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#string.prototype.fontcolor" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">String.prototype.fontcolor</small></a></font></font></td>
<td><font><font><span class="spec-Standard">Standard</span></font></font></td>
<td><font><font>初始定义。</font><font>实现在JavaScript中1.0。</font><font>定义为附加功能的ECMAScript针对Web浏览器（规范）附件B。</font></font></td>
</tr>
<tr>
<td><font><font><a class="external" href="https://tc39.github.io/ecma262/#string.prototype.fontcolor" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">String.prototype.fontcolor</small></a></font></font></td>
<td><font><font><span class="spec-Draft">Draft</span></font></font></td>
<td><font><font>定义为附加功能的ECMAScript针对Web浏览器（规范）附件B。</font></font></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性"><font><font>浏览器兼容性</font></font></h2>
<div><font><font><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></font></font></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th><font><font>Firefox(Gecko)</font></font></th>
<th>Internet Explorer</th>
<th>Opera</th>
<th><font><font>Safari</font></font></th>
</tr>
<tr>
<td>Basic Support</td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
<td><font><font>1.0 (1.7 or earlier)</font></font></td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th><font><font>Android</font></font></th>
<th><font><font>Chrome for Android</font></font></th>
<th><font><font>Firefox Mobile(Gecko)</font></font></th>
<th><font><font>IE Mobile</font></font></th>
<th><font><font>Opera Mobile</font></font></th>
<th><font><font>Safari Mobile</font></font></th>
</tr>
<tr>
<td>Basic Support</td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
<td><font><font>1.0 (1.0)</font></font></td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
<td><font><font><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></font></font></td>
</tr>
</tbody>
</table>
</div>
<h2 id="也可以看看"><font><font>也可以看看</font></font></h2>
<ul>
<li><font><font><a href="Reference/Global_Objects/String/fontsize" title="The fontsize() method creates a &lt;font&gt; HTML element that causes a string to be displayed in the specified font size."><code>String.prototype.fontsize()</code></a></font></font></li>
</ul>
</article>